// pages/coupon_order/couponOrder.js
// 用户下单发红包的领取页面
const app = getApp()
const Utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareCode: '',
    isExpand: false,
    rotate: 'rotate',
    myAccept: {},
    msg: '',
    acceptList: []
  },
  toggleExpand () {
    let isExpand = this.data.isExpand
    let rotate = !isExpand ? 'rotate' : ''
    this.setData({
      isExpand: !isExpand,
      rotate: rotate
    })
  },
  viewMyCoupon () {
    wx.navigateTo({
      url: '/pages/middlewave/middlewave?href=' + encodeURIComponent(app.globalData.frontUrl + 'coupon'),
    })
  },
  getMyCoupon() {
   //我领取的红包
   wx.request({
     url: app.globalData.baseUrl + '/boon/records',
     data: {shareCode: this.data.shareCode},
     header: {'content-Type': 'application/x-www-form-urlencoded'},
     method: 'POST',
     success: (res) => {
       if (res.code === 0) {
         this.setData({
           myAccept: res.data
         })
       }
       else {
         this.setData({
           msg: res.msg
         })
       }
       
     },
     fail: () => {

     }
   })
  },
  getcouponList () {
    //获取用户领取列表
    wx.request({
      url: app.globalData.baseUrl + '/boon/gainBoon',
      method: 'POST',
      data: { shareCode: this.data.shareCode },
      header: {'content-Type': 'application/x-www-form-urlencoded'},
      success: (res) => {
        if (res.code === 0) {
          let list = res.data.map((v, k) => {
            v.gainTimeStr = Utils.formatTime(v.gainTime, '-', '-', '', ':')
            return v
          })
          this.setData({
            acceptList: list
          })
        }
        
      },
      fail: () => {

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let shareCode = options.shareCode
    console.log(shareCode)
    if (shareCode) {
      this.setData({
        shareCode: shareCode
      })
      this.getMyCoupon()
      this.getcouponList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})